package com.xunsiya.controller.system;

import com.alibaba.fastjson.JSONObject;
import com.xunsiya.common.config.RequestUtils;
import com.xunsiya.common.config.util.ConstantInfo;
import com.xunsiya.service.system.CompanyService;
import com.xunsiya.system.vo.Company;
import com.xunsiya.system.vo.User;
import com.xunsiya.tools.common.msg.Msg;
import com.xunsiya.tools.common.pagination.ForePage;
import com.xunsiya.tools.common.string.StringUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 分组控制层
 *
 * @author linqy
 * @version 1.0
 *          <p/>
 *          <br/>
 *          <br/>修订人    修订时间      描述信息
 *          <br/>-----------------------------------------------------
 *          <br/>linqy    2017/11/11    初始创建
 */

@RestController
@RequestMapping("/api/company")
public class CompanyController {

    private Logger logger = Logger.getLogger(CompanyController.class);

    private static final String SYMBOL = ",";

    @Autowired
    private CompanyService companyService;

    /**
     *群组列表 分页查询
     * @param company
     * @return
     *
     * TODO 这个方法返回的结构有分页有不分页两种结构，需合并结构或者拆分方法
     */
    @PostMapping
    public Msg queryCompanyList(@RequestBody Company company,
                                @RequestParam(value="pageIndex",required = false,defaultValue = "1")Integer pageIndex,
                                @RequestParam(value="pageSize",required = false,defaultValue = "10") Integer pageSize) {
        User user = RequestUtils.getSessionAttribte(User.SYS_LOGIN_INFO);
        if (company != null && !StringUtil.isEmpty(company.getCompanyName())) {
            company.setCompanyName("%" + company.getCompanyName() + "%");
        }
        if(user.getCompanyId().equals(ConstantInfo.PLATFORM_COPMANY_ID)) {
            return companyService.queryCompanyForPage(company, pageIndex, pageSize);
        }
        return companyService.queryCompany(user.getCompanyId());
    }

    @PostMapping("/query")
    public Msg query() {
        User user = RequestUtils.getSessionAttribte(User.SYS_LOGIN_INFO);
        Msg msg = new Msg();
        if(user.getCompanyId().equals(ConstantInfo.PLATFORM_COPMANY_ID)) {
            Msg allCompanyMsg = companyService.queryCompanyForPage(new Company(), 1, Integer.MAX_VALUE);
            msg.setMsg(allCompanyMsg.getMsg());
            msg.setSuccess(allCompanyMsg.isSuccess());
            ForePage data = (ForePage)allCompanyMsg.getData();
            msg.setData(data.getList());
        }else {
            msg = companyService.queryCompany(user.getCompanyId());
        }
        return msg;
    }

    /**
     * 新增群组
     * @param company
     * @return
     * @throws Exception
     */
    @PostMapping("/insert")
    public Msg addCompany(@RequestBody Company company) throws Exception {
        return companyService.insertCompany(company);
    }

    /**
     * 修改群组
     * @param company
     * @return
     */
    @PostMapping("/update")
    public Msg modifyCompany(@RequestBody Company company) {
        return companyService.modifyCompany(company);
    }

    /**
     * 删除群组
     * @param requestBody
     * @return
     */
    @PostMapping("/delete")
    public Msg deleteCompany(@RequestBody JSONObject requestBody) {
        int id = requestBody.getInteger("id");
        return companyService.deleteCompany(id);
    }

    /**
     * 查询群组
     * @return
     */
    @PostMapping("/querys")
    public Msg queryCompany() {
        User user = RequestUtils.getSessionAttribte(User.SYS_LOGIN_INFO);
        //管理员登录时展现所有群组
        if(user.getCompanyId().equals(ConstantInfo.PLATFORM_COPMANY_ID)) {
            return companyService.queryAllCompany();
        }
        //非管理员登录时展现本群组及所有下级群组
        return companyService.getCompanyByUser(user.getCompanyId());
    }

    /**
     * 查询所有的企业
     * @return
     */
    @GetMapping("/findAll")
    public List<Company> findAll() {
        return companyService.findAll();
    }

    @PostMapping("/getParentId")
    public Company findParentId(){
        User user = RequestUtils.getSessionAttribte(User.SYS_LOGIN_INFO);
        return companyService.findParentId(user.getCompanyId());
    }
}
